package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import org.linlinjava.litemall.db.dao.LitemallRefundMapper;
import org.linlinjava.litemall.db.dao.RefundMapper;
import org.linlinjava.litemall.db.domain.LitemallRefund;
import org.linlinjava.litemall.db.domain.LitemallRefundExample;
import org.linlinjava.litemall.db.domain.RefundStatisticsVo;
import org.linlinjava.litemall.db.domain.ShopRefundListQuery;
import org.linlinjava.litemall.db.domain.ShopRefundListVo;
import org.linlinjava.litemall.db.domain.TotalRefundStatisticsVo;
import org.linlinjava.litemall.db.domain.TotalShopRefundStatisticsVo;
import org.springframework.stereotype.Service;

 

@Service
public class LitemallRefundService extends MPJBaseServiceImpl<LitemallRefundMapper,LitemallRefund> {
	
    @Resource
    private LitemallRefundMapper litemallRefundMapper;
    @Resource
    private RefundMapper refundMapper;
    
    public LitemallRefund findById(Integer id) {
    	return litemallRefundMapper.selectByPrimaryKey(id);
    }

    public void add(LitemallRefund refund) {
    	LocalDateTime addDate = LocalDateTime.now();
		int year = addDate.getYear();
		int month = addDate.getMonth().getValue();
    	refund.setAddTime(addDate);
    	refund.setYear(year);
    	refund.setMonth(month);
    	litemallRefundMapper.insertSelective(refund);
    }
    
    public void update(LitemallRefund refund) {
    	litemallRefundMapper.updateByPrimaryKeySelective(refund);
    }
    
    /**
     * 查询用户此次的退款记录
     * @param userId
     * @param minTime
     * @return
     */
    public List<LitemallRefund> queryAfterTimeByUserIdAndRefundType(Integer userId, LocalDateTime minTime, Byte refundType) {
    	LitemallRefundExample example = new LitemallRefundExample();
    	LitemallRefundExample.Criteria criteria = example.createCriteria();
    	
    	if(null != userId) {
    		criteria.andAddUserIdEqualTo(userId);
    	}
    	if(null != refundType) {
    		criteria.andRefundTypeEqualTo(refundType);
    	}
    	if(null != minTime) {
    		criteria.andAddTimeGreaterThanOrEqualTo(minTime);
    	}
    	criteria.andDeletedEqualTo(false);
    	return litemallRefundMapper.selectByExample(example);
    }
    
    /**
     * 统计退款
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param regionId 区域id
     * @return
     */
    public List<RefundStatisticsVo> statistics(String startTime, String endTime, Integer regionId, Integer page, Integer limit) {
    	//PageHelper.startPage(page, limit);
    	return refundMapper.statistics(startTime, endTime, regionId);
    }
    
    /**
     * 汇总统计
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param regionId 区域id
     * @return
     */
    public TotalRefundStatisticsVo totalStatistics(String startTime, String endTime, Integer regionId) {
    	return refundMapper.totalStatistics(startTime, endTime, regionId);
    }
    
    /**
     * 统计代理退款
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param regionId 区域id
     * @return
     */
    public List<RefundStatisticsVo> proxyStatistics(String startTime, String endTime, Integer regionId, Integer page, Integer limit) {
    	//PageHelper.startPage(page, limit);
    	return refundMapper.proxyStatistics(startTime, endTime, regionId);
    }
    
    /**
     * 汇总统计
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param regionId 区域id
     * @return
     */
    public TotalRefundStatisticsVo proxyTotalStatistics(String startTime, String endTime, Integer regionId) {
    	return refundMapper.proxyTotalStatistics(startTime, endTime, regionId);
    }
    
    /**
     * 退款列表
     * @param year 年
     * @param month 月
     * @param shopId 店铺id
     * @return
     */
    public IPage<ShopRefundListVo> shopRefundList(ShopRefundListQuery query, Integer page, Integer limit) {
    	//PageHelper.startPage(page, limit);
    	return refundMapper.shopRefundList(new Page<>(page,limit),query);
    }
    
    /**
     * 店铺退款汇总统计
     * @param query 店铺退款汇总
     * @return
     */
    public TotalShopRefundStatisticsVo totalShopStatistic(ShopRefundListQuery query) {
    	return refundMapper.totalShopStatistic(query);
    }
    
    /**
     * 根据外部退款id查询退款记录
     * @param outRefundId
     * @return
     */
    public LitemallRefund queryByOutRefundId(String outRefundId) {
    	LitemallRefundExample example = new LitemallRefundExample();
    	LitemallRefundExample.Criteria criteria = example.createCriteria();
    	criteria.andOutRefundIdEqualTo(outRefundId).andDeletedEqualTo(false);
    	return litemallRefundMapper.selectOneByExample(example);
    }

	public LitemallRefund queryByOrderSn(String orderSn,Integer userId) {
		LitemallRefundExample example = new LitemallRefundExample();
		LitemallRefundExample.Criteria criteria = example.createCriteria();
		criteria.andOrderSnEqualTo(orderSn).andDeletedEqualTo(false);

		if(userId == null){
			criteria.andUserIdEqualTo(userId);
		}
		return litemallRefundMapper.selectOneByExample(example);
	}
}
